package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2024-01-11
 * Time: 9:37
 */
//空接口/标记接口：作用就是表示当前对象是可以被克隆的
//    Object类是所有类的父类
class Money implements Cloneable {
    public double money;

    @Override
    protected Object clone() throws CloneNotSupportedException {
//      必须要通过重写来调用Object类的clone方法，因为该方法时protected修饰的，不同包下只能通过super访问
        return super.clone();
    }
}
class Student implements Cloneable{
    public String name;
    public Money m = new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", m=" + m.money +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
//        这里只是克隆了student对象
        Student student = (Student)super.clone();//实现了深拷贝
//        这里克隆了student对象内的money对象
        student.m = (Money)this.m.clone();
        return student;
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student();
        student.name = "lisi";
        Student student1 = (Student)student.clone();
        student1.m.money = 12.5;
        System.out.println(student);
        System.out.println(student1);
    }
}
